$(function () {
  sanlanzhang = 1;
  initsanlanzhangData();
})
//传值(项目编号)
let projectCode = localStorage.getItem('projectCode');
console.log(projectCode, 'projectCode')

//项目名称
let projectName = localStorage.getItem('projectName');
console.log(projectName, 'xiangmumingc')
//名字
let personName = localStorage.getItem('name');
console.log(personName, '名字')

//
let Name = localStorage.getItem('Name');
console.log(Name)

console.log(projectCode, projectName, personName, '传值')
if (projectCode) {
  $(".projectCode").text(projectCode);
} else {
  $(".projectCode").text('-');
}
if (projectName) {
  $(".projectName").text(projectName);
} else {
  $(".projectName").text('暂无项目名称');
}
if (personName) {
  $(".personName").text(personName);
} else {
  $(".personName").text('暂无负责人');
}
if (Name) {
  $(".Name").text(Name);
} else {
  $(".Name").text('-');
}

let page = 1;
let limit = 10;
let cols = [
  // 1
  [{
      field: '',
      title: '本年',
      colspan: 2,
      align: 'center',
      width: 120,
    }, {
      field: 'key05',
      title: '凭证号',
      rowspan: 2,
      width: 200,
      sort: true,
      align: 'right',
    },
    {
      field: 'key06',
      rowspan: 2,
      title: '摘要',
      align: 'left',
    },
    {
      field: 'key07',
      rowspan: 2,
      width: 140,
      title: '科目编号',
      sort: true,
      align: 'right',
    },
    {
      field: 'key08',
      rowspan: 2,
      title: '科目名称',
      align: "left",
    },
    {
      field: 'key09',
      width: 100,
      rowspan: 2,
      title: '借方',
      sort: true,
      align: 'right',
      templet: function (d) {
        return `<span>${pricefmt((d.key09 / 10000).toFixed(2))}</span>`
      }
    },
    {
      field: 'key10',
      width: 100,
      rowspan: 2,
      title: '贷方',
      sort: true,
      align: 'right',
      templet: function (d) {
        return `<span>${pricefmt((d.key10 / 10000).toFixed(2))}</span>`
      }
    },
    {
      field: 'key11',
      width: 60,
      rowspan: 2,
      title: '方向',
      align: "center"
    },
    {
      field: 'key12',
      rowspan: 2,
      title: '余额',
      sort: true,
      align: 'right',
      templet: function (d) {
        return `<span>${pricefmt((d.key12 / 10000).toFixed(2))}</span>`
      }
    }
  ],
  // 2
  [{
      field: 'key03',
      title: '月',
      sort: true,
      width: 60,
      align: 'center',
    },
    {
      field: 'key04',
      title: '日',
      sort: true,
      width: 60,
      align: 'center',
    }
  ],

];

function initsanlanzhangData() {
  const username = localStorage.getItem("user");
  const currYear = localStorage.getItem("year");
  const currMon = localStorage.getItem("month");
  const currDept = localStorage.getItem("dept");
  var obj = {
    username: username,
    budgetYear: currYear,
    budgetMonth: currMon,
    dept: currDept,
    status: 2,
    chartPage: 60201,
    chartNo: 2,
    key02: projectCode,

    pageNum: page, //页号
    pageRowCount: limit, //每页多少条
  };
  ApiGet("getdata", obj, function (e) {
    var json = JSON.parse(e);
    var chartData = json.items[0].charts;
    console.log(chartData, "三栏账数据");
    let data = chartData[0].data
    tableDrawing(data) // 数据传到 table组件
    pagesDrawing(data) // 数据传到 分页组件
  });
}

function tableDrawing(data) {
  layui.use('table', function () {
    let table = layui.table;
    table.render({
      elem: '#newsList',
      cols: cols,
      data: data, // 数据
      limit: limit, // 每页显示的条数
      // height: tableHeight,
    });
  })
}

function pagesDrawing(data) {
  let totolPage = data[0].key13
  layui.use('laypage', function () {
    let laypage = layui.laypage;
    laypage.render({
      elem: 'laypage', //注意，这里的 page 是 ID，不用加 # 号
      count: totolPage, //数据总数，从服务端得到
      theme: '#E8F3FF',
      limit: limit, // 每页条数
      prev: '<em><</em>',
      next: '<em>></em>',
      layout: ['count', 'prev', 'page', 'next', 'limit', 'skip'],
      jump: function (obj, first) {
        console.log(obj, '第几页 ')
        console.log(obj.curr, '第几页 ')
        //首次不执行
        if (!first) {
          limit = obj.limit;
          numpage(obj.curr, obj.limit) // 分页点击传参 
        }
      }
    });
  })
};

function numpage(curr, page_count) { // 分页切换-数据请求
  const username = localStorage.getItem("user");
  const currYear = localStorage.getItem("year");
  const currMon = localStorage.getItem("month");
  const currDept = localStorage.getItem("dept");
  var obj = {
    username: username,
    budgetYear: currYear,
    budgetMonth: currMon,
    dept: currDept,
    status: 2,
    chartPage: 60201,
    chartNo: 2,
    key02: projectCode,

    pageNum: curr, //页号
    pageRowCount: page_count, //每页多少条
  };
  ApiGet("getdata", obj, function (e) {
    var json = JSON.parse(e);
    var chartData = json.items[0].charts;
    console.log(chartData, "三栏账数据");
    let data = chartData[0].data
    tableDrawing(data) // 数据传到 table组件
    // pagesDrawing(data) // 数据传到 分页组件
  });
};